<?php

//小站选项应用模型
class Plugins_model extends Model {

	function Plugins_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("options_model");
	}
	
	//小站模块详细
	function get_module($modid)
	{
		$result = array();
		$query = $this->db->get_where("modules", array("id"=>$modid));
		if($query->num_rows()>0) {
			$result = $query->row_array();
		}
		return $result;
	} 
	
	//小站模块列表
	function get_modules()
	{
		$result = array();
		$query = $this->db->get("modules");
		if($query->num_rows()>0) {
			$result = $query->result_array();
		}
		return $result;
	}
	
	//添加选项应用
	function add_plugins($opid, $plname, $moduleid)
	{
		$data = array(
			"opid" => $opid,
			"plname" => $plname,
			"moduleid" => $moduleid,
			"addtime" => time(),
		);
		if($moduleid == 1 ||  $moduleid == 3) {
			$data["public"] = 0;
		}
		$result = $this->db->insert("site_plugins", $data);
		$plid = $this->db->insert_id();
		return $plid;
	}
	
	//获取小站选项应用列表
	function get_oplugins($opid)
	{
		$oplugins = array();
		$query = $this->db->from("site_plugins")->where("opid", $opid)->order_by("id","desc")->get();
		if($query->num_rows()>0) {
			$oplugins = $query->result_array();
		}
		return $oplugins;
	}
	
	//编辑选项应用
	function edit_plugin($plid, $plname, $public = 0)
	{
		$data = array(
			"plname" => $plname,
			"addtime" => time(),
			"public" => $public,
		);
		$this->db->where("id", $plid);
		$result = $this->db->update("site_plugins", $data);
		return $result;
	}
	
	//删除选项应用
	function del_plugin($plid)
	{
		//删除应用内容
		$this->db->where('plid', $plid);
		$this->db->delete('module_hot');
		$this->db->where('plid', $plid);
		$this->db->delete('module_news');
		$this->db->where('plid', $plid);
		$this->db->delete('module_board');
		$this->db->where('plid', $plid);
		$this->db->delete('module_social');
		$this->db->where('plid', $plid);
		$this->db->delete('module_vote');
		$this->db->where('plid', $plid);
		$this->db->delete('module_special');
		$this->db->where('plid', $plid);
		$this->db->delete('module_album');
		$this->db->where('plid', $plid);
		$this->db->delete('module_video');
		
		$result = $this->db->delete("site_plugins", array("id"=>$plid));
		return $result;
	}
	
	//获取应用
	function get($plid)
	{
		$result = array();
		$query = $this->db->get_where("site_plugins", array("id"=>$plid));
		if($query->num_rows()>0) {
			$result = $query->row_array();
		}
		return $result;
	}
	
	//获取应用对应小站
	function get_siteid($plid)
	{
		$row = $this->get($plid);
		if(!$row) return false;
		$row = $this->options_model->get($row["opid"]);
		if ($row) {
			return $row["siteid"];
		} else {
			return false;
		}
		
	}
}

/* End of file */